<script lang="ts" setup>
import {computed} from 'vue';

import {Settings} from '@/plugins/iconify';
import {$t, loadLocaleMessages} from '@/locales/effects';
import {preferences, updatePreferences} from '@/preferences/effects';
import {capitalizeFirstLetter} from '@/utils/tool';
import {useJcxDrawer} from '@/components/common/popup';
import {JcxButton} from '@/components/common/shadcn';

import PreferencesDrawer from './preferences-drawer.vue';

const [Drawer, drawerApi] = useJcxDrawer({
  connectedComponent: PreferencesDrawer,
});

/**
 * preferences 转成 vue props
 * preferences.widget.fullscreen=>widgetFullscreen
 */
const attrs = computed(() => {
  const result: Record<string, any> = {};
  for (const [key, value] of Object.entries(preferences)) {
    for (const [subKey, subValue] of Object.entries(value)) {
      result[`${key}${capitalizeFirstLetter(subKey)}`] = subValue;
    }
  }
  return result;
});

/**
 * preferences 转成 vue listener
 * preferences.widget.fullscreen=>@update:widgetFullscreen
 */
const listen = computed(() => {
  const result: Record<string, any> = {};
  for (const [key, value] of Object.entries(preferences)) {
    if (typeof value === 'object') {
      for (const subKey of Object.keys(value)) {
        result[`update:${key}${capitalizeFirstLetter(subKey)}`] = (
            val: any,
        ) => {
          updatePreferences({[key]: {[subKey]: val}});
          if (key === 'app' && subKey === 'locale') {
            loadLocaleMessages(val);
          }
        };
      }
    } else {
      result[key] = value;
    }
  }
  return result;
});
</script>
<template>
  <div>
    <Drawer v-bind="{ ...$attrs, ...attrs }" v-on="listen"/>

    <div @click="() => drawerApi.open()">
      <slot>
        <JcxButton
            :title="$t('preferences.title')"
            class="bg-primary flex-col-center size-10 cursor-pointer rounded-l-lg rounded-r-none border-none"
        >
          <Settings class="size-5"/>
        </JcxButton>
      </slot>
    </div>
  </div>
</template>
